//
//  QuotationCenterController.m
//  Stock
//
//  Created by blacksun on 11-7-3.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "QuotationCenterController.h"
#import "BaseLevel1Controller.h"
#import "MarketBriefController.h"


@implementation QuotationCenterController
@synthesize table1;
@synthesize table2;
@synthesize table1Data;
@synthesize table2Data;
@synthesize quotationCenterService;


#pragma mark -Service Delegate
-(void)requestFinishedWithResult:(id)aResult andUserinfo:(NSDictionary *)aUserinfo{
    NSString *action = [aUserinfo objectForKey:kASIHttpRequestUserinfoKey];
    if ([kQuotationCenterServiceMarketBrief isEqualToString:action]) {
        self.table1Data = aResult;
        [self.table1 reloadData];
    }
}
-(void)requestFinishedwithError:(NSError *)aError andUserinfo:(NSDictionary *)aUserinfo{
    
}

- (void)dealloc
{
    self.table1Data = nil;
    self.table2Data = nil;
    self.table1 = nil;
    self.table2 = nil;
    [quotationCenterService cancleAllRequest];
    self.quotationCenterService = nil;
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.quotationCenterService = [[[QuotationCenter alloc] initWithDelegate:self] autorelease];
    
    self.table2Data = [NSArray arrayWithObjects:@"行业",@"概念板块",@"涨跌排行",nil];
    
    [self.navigationController setNavigationBarHidden:NO];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationItem.titleView = [StockAppHelper titleViewForNavigationItemWithString:self.title];
    
}
-(void)viewWillAppear:(BOOL)animated{
    
    MyStock *stock1 = [[[MyStock alloc] init] autorelease];
    MyStock *stock2 = [[[MyStock alloc] init] autorelease];
    MyStock *stock3 = [[[MyStock alloc] init] autorelease];
    stock1.name = @"上证指数";
    stock2.name = @"深证成指";
    stock3.name = @"沪深300";
    
    self.table1Data = [NSArray arrayWithObjects:stock1,stock2,stock3, nil];
    [self.table1 reloadData];
    [quotationCenterService queryMarketBrief];
    
    StockAppDelegate *delegate = [UIApplication sharedApplication].delegate;
    delegate.navigationController = self.navigationController;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.table1 = nil;
    self.table2 = nil;
    [quotationCenterService cancleAllRequest];
    self.quotationCenterService = nil;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    /*if (tableView == table1) {
        return 2;
    }*/
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"QuotationCenterCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        if (tableView == table1) {
            cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"high_cell1.png"]] autorelease];
            cell.accessoryType = UITableViewCellAccessoryNone;
            
            UIColor *clearColor = [UIColor clearColor];
            
            UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 20.0, 70.0, 20.0)];
            nameLabel.backgroundColor = clearColor;
            nameLabel.textColor = [UIColor whiteColor];
            nameLabel.tag = kStockNameTag;
            UILabel *todayPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(85.0, 20.0, 90.0, 20.0)];
            todayPriceLabel.backgroundColor = clearColor;
            todayPriceLabel.tag = kStockTodayPriceTag;
            todayPriceLabel.textAlignment = UITextAlignmentRight;
            UILabel *changePriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(180.0, 20.0, 60.0, 20.0)];
            changePriceLabel.backgroundColor = clearColor;
            changePriceLabel.tag = kStockChangePriceTag;
            changePriceLabel.textAlignment = UITextAlignmentRight;
            UILabel *changeRateLabel = [[UILabel alloc] initWithFrame:CGRectMake(245.0, 20.0, 65.0, 20.0)];
            changeRateLabel.backgroundColor = clearColor;
            changeRateLabel.tag = kStockChangeRateTag;
            changeRateLabel.textAlignment = UITextAlignmentRight;
            
            [cell.contentView addSubview:nameLabel];
            [cell.contentView addSubview:todayPriceLabel];
            [cell.contentView addSubview:changePriceLabel];
            [cell.contentView addSubview:changeRateLabel];
            
            [nameLabel release];
            [todayPriceLabel release];
            [changePriceLabel release];
            [changeRateLabel release];

            
        }else{
            cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"high_cell2.png"]] autorelease];
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.accessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right_extend.png"]] autorelease];
        }
        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (tableView == table2) {
        cell.textLabel.font = [UIFont systemFontOfSize:17.0];
        cell.textLabel.text = [self.table2Data objectAtIndex:indexPath.row];
    }else{
        
        MyStock *currentStock = [table1Data objectAtIndex:indexPath.row];
        UILabel *nameLabel = (UILabel *)[cell viewWithTag:kStockNameTag];
        UILabel *todyLabel = (UILabel *)[cell viewWithTag:kStockTodayPriceTag];
        UILabel *priceLabel = (UILabel *)[cell viewWithTag:kStockChangePriceTag];
        UILabel *rateLabel = (UILabel *)[cell viewWithTag:kStockChangeRateTag];
        
        UIColor *textColor = nil;
        if(!currentStock.openingPrice || currentStock.openingPrice == 0){
            textColor = [UIColor grayColor];
        }else if ([currentStock.changedPrice hasPrefix:@"-"]) {
            textColor = [UIColor greenColor];
        }else{
            textColor = [UIColor redColor];
        }
        
        todyLabel.textColor = textColor;
        priceLabel.textColor = textColor;
        rateLabel.textColor = textColor;
        nameLabel.text = currentStock.name;
        if (currentStock.openingPrice && currentStock.openingPrice != 0) {
            todyLabel.text = [currentStock.closingPrice stringValue];
            priceLabel.text = currentStock.changedPrice;
            rateLabel.text = currentStock.changedRate;    
        }else{
            todyLabel.text = @"--";
            priceLabel.text = @"--";
            rateLabel.text = @"--";  
        }
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView==table2) {
        if (indexPath.row==2) {
            ItemListController *childController = [[ItemListController alloc] initWithNibName:@"ItemListController" bundle:nil];
            childController.title = [table2Data objectAtIndex:indexPath.row];
            [self.navigationController pushViewController:childController animated:YES];
            [childController release];
            return;
        }
        BaseLevel1Controller *level1Controller = [[BaseLevel1Controller alloc] initWithNibName:@"Level1List" bundle:nil];
        level1Controller.title = [table2Data objectAtIndex:indexPath.row];
        if (indexPath.row == 0) {
            level1Controller.requestUrl = @"jf/market/industry.jf";
            level1Controller.type = kIndustryTypeKey;
        }else if(indexPath.row == 1){
            level1Controller.requestUrl = @"jf/market/concept.jf";
            level1Controller.type = kConceptTypeKey;
        }
        [self.navigationController pushViewController:level1Controller animated:YES];
        [level1Controller release];
    }else{
        MyStock *currentStock = [table1Data objectAtIndex:indexPath.row];
        MarketBriefController *chartController = [[MarketBriefController alloc] initWithNibName:@"MarketBriefController" bundle:nil];
        chartController.title = currentStock.name;
        chartController.stock = currentStock;
        [self.navigationController pushViewController:chartController animated:YES];
        [chartController release];
    }
}

@end